package com.fysports.web.controller.system;

import com.jfinal.plugin.activerecord.Db;
import com.fysports.web.controller.AbstractBaseController;
import com.fysports.web.controller.engine.RestFulController;
import com.fysports.web.kit.commons.PathBizKit;
import com.fysports.web.model.system.SystemMenu;
import com.fysports.web.module.core.DefaultModuleBuilder;
import com.fysports.web.module.core.IModuleFactory;
import com.fysports.web.module.view.JsonView;
import com.fysports.web.module.view.OperateType;


/**
 * 菜单管理Controller
 * Date : 14-8-5 下午12:53
 * Project : JdmData_WebServer
 */
public class ManagerMenuController extends AbstractBaseController implements RestFulController {
    IModuleFactory moduleFactory = new DefaultModuleBuilder();


    public void index() {
        setAttr("list", SystemMenu.dao.search(SystemMenu.COL_LIVEFLAG, "1"));
        moduleFactory.create(this);
        execute();
        render(PathBizKit.getPageAdminPath("system/menu/menu_list.ftl"));
    }

    @Override
    public void add() {
        Integer maxcode= Db.queryInt("select max(code) from sys_menu");
        /*
        *  code 自增，不采用主键防止误删后恢复，因主键不能回退
        * */
        SystemMenu sm = new SystemMenu();
        sm.setCode(String.valueOf(maxcode+1));
        setAttr("m", sm);
        render(PathBizKit.getPageAdminPath("system/menu/menu_add.ftl"));
    }

    @Override
    public void edit() {
        String uuid = getPara();
        SystemMenu sm = SystemMenu.dao.findById(uuid);
        setAttr("m", sm);
        render(PathBizKit.getPageAdminPath("system/menu/menu_update.ftl"));
    }

    @Override
    public void save() {
        SystemMenu sm = getModel(SystemMenu.class, "m");
        boolean status = sm.save();
        JsonView json = new JsonView();
        json.setStatus(status);
        json.setMsg(status ? OperateType.SAVE_SUCCESS : OperateType.SAVE_FAIL);
        renderJson(json);
    }

    @Override
    public void update() {
        SystemMenu sm = getModel(SystemMenu.class, "m");
        boolean status = sm.update();
        JsonView json = new JsonView();
        json.setStatus(status);
        json.setMsg(status ? OperateType.UPDATE_SUCCESS : OperateType.UPDATE_FAIL);
        renderJson(json);
    }

    @Override
    public void delete() {
        String uuid = getPara();
        JsonView json = new JsonView();
        SystemMenu sm = SystemMenu.dao.findById(uuid);
        boolean status = sm.delete();
        json.setStatus(status);
        json.setMsg(status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
        renderJson(json);
    }
}
